<?php

// [ 配置类 ]

namespace core;

class Config
{
    // 配置参数
    private static $_config = [];

    // 初始化配置
    public static function init()
    {
        $files = scandir(CONFIG_PATH);
        foreach ($files as $file) preg_match('/(.*)\.php$/', $file, $ret) && self::set(CONFIG_PATH . $file, $ret[1]);
    }

    // 加载配置
    public static function set($uri, $range)
    {
        self::$_config[$range] = include $uri;
    }

    // 读取配置（无限级）
    public static function get($name = '')
    {
        if ( empty($name) ) {
            return self::$_config;
        } else {
            if ( strpos($name, '.') ) {
                $arr = explode('.', $name);
                $config = self::$_config;
                foreach ($arr as $v) {
                    $config = isset($config[$v]) ? $config = $config[$v] : false;
                    if (!$config) break;
                }
                return $config;
            } else {
                return isset( self::$_config[$name] ) ? self::$_config[$name] : false;
            }
        }
    }
}
